package jdom;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.IOException;
import java.util.List;

public class TestJdomParser {

    public static void main(String[] args) {

        //创建读取xml的工具
        SAXBuilder builder=new SAXBuilder();


        try {
            Document document=builder.build(TestJdomParser.class
                                     .getClassLoader()
                                     .getResourceAsStream("students.xml"));


            Element classElement=document.getRootElement();

            System.out.println(classElement);

            System.out.println(classElement.getName());

            String no=classElement.getAttributeValue("no");

            System.out.println(no);

            List<Element> studentElements=classElement.getChildren("student");

            for (Element studentElement : studentElements) {
                List<Attribute> attributes=studentElement.getAttributes();

                for (Attribute attribute : attributes) {
                    System.out.println(attribute.getName()+"="+attribute.getValue());
                }

//                Element nameElement=studentElement.getChild("name");
//
//                System.out.println(nameElement.getText());

                System.out.println(studentElement.getChildText("name"));

            }



        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
